package com.git.contractmanagement.config;

import cn.dev33.satoken.stp.StpInterface;
import com.git.contractmanagement.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AuthorizationConfig implements StpInterface {

    @Resource
    private UserService userService;

    /**
     * 返回用户拥有的角色列表
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        Long userId = Long.valueOf(loginId.toString());
        String role = userService.getRoleByUserId(userId);
        BaseContext.setCurrentId(userId);
        if (role != null) {
            return List.of(role); // 返回角色列表
        }
        return new ArrayList<>();
    }

    /**
     * 返回用户拥有的权限列表
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        Long userId = Long.valueOf(loginId.toString());
        String role = userService.getRoleByUserId(userId);
        List<String> permissions = new ArrayList<>();
        // 根据角色分配权限（可改为从数据库读）
        switch (role) {
            case "ADMIN":
                permissions.add("user:add");
                permissions.add("user:update");
                permissions.add("user:delete");
                permissions.add("user:view");
                break;
            case "USER":
                permissions.add("user:view");
                permissions.add("user:update");
                break;
        }
        return permissions;
    }
}